<?php
require_once 'Zend/Rest/Client.php';
class Kornak_Service_Notifications
{
    const API_HOST = 'https://www.appnotifications.com';
	const API_PATH = '/account/notifications';

	protected $_apiKey;
	protected $_restClient;

	public function __construct($apiKey, $restClient = null)
	{
		$this->setApiKey($apiKey);
		if (null !== $restClient) {
			$this->setRestClient($restClient);
		}
	}

	public function getApiKey()
	{
		return $this->_apiKey;
	}

    public function getRestClient()
    {
    	if (null === $this->_restClient) {
    		$this->_restClient = new Zend_Rest_Client();
    	}
        return $this->_restClient;
    }

    public function sendNotification($notification)
    {
    	$restClient = $this->getRestClient();

        $data = array(
            'user_credentials' => $this->_apiKey,
            'notification'     => array(
            ),
        );

        $allowedParameters = array(
            'message',
            'longMessage',
            'title' => '',
            'longMessagePreview' => '',
            'silent' => '',
            'actionLocKey' => '',
            'runCommand' => '',
            'sound' => '',
            '' => '',
        );
        foreach ($allowedParameters as $source => $destination) {
			if (!empty($notification[$source])) {
			    $data[$destination] = $notification[$source];
			}
        }
                'message'              => $notification['message'],
                'long_message'         => $notification['longMessage'],
                'title'                => $notification['title'],
                'long_message_preview' => $notification['longMessagePreview'],
                'silent'               => $notification['silent'],
                'action_loc_key'       => $notification['actionLocKey'],
                'run_command'          => $notification['runCommand'],
                'sound'                => $notification['sound'],


        return $restClient->setUri(self::API_HOST)
                          ->restPost(self::API_PATH, $data);
    }

    public function setApikey($value)
    {
        $this->_apiKey = (string) $value;
    }

    public function setRestClient(Zend_Rest_Client $value)
    {
        $this->_restClient = $value;
    }
}
